﻿using UnityEngine;

namespace Core
{
    public class CameraFollow : Core.Component,
        IAwake, IAwake<Transform>, ILateUpdate
    {
        public Transform Target { get; private set; }

        public bool Enabled;

        public void Awake()
        {
            Enabled = true;
        }

        public void Awake(Transform target)
        {
            Target = target;
            Enabled = true;
        }

        public void Follow(Transform trans)
        {
            if (trans == null)
            {
                Enabled = false;
                return;
            }

            Target = trans;
            Enabled = true;
        }

        public void LateUpdate()
        {
            if (Enabled == false || Target == null || Camera.main == null)
            {
                return;
            }

            var transform = Camera.main.transform;
            var pos = Target.position;
            pos.z = transform.position.z;
            transform.position = pos;
        }
    }
}